using System;

namespace TPL.Xna.Common
{
    public class InfoScreen : Screen
    {
        private int _totalSeconds;
        private readonly Screen _nextScreen;
        private bool _startCalculating;
        private DateTime _lastTime;

        public InfoScreen(int displayTime, Screen nextScreen)
        {
            _totalSeconds = displayTime;
            _nextScreen = nextScreen;
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (!_startCalculating)
                _startCalculating = true;
            else
            {
                var n = DateTime.Now;
                var d = n.Subtract(_lastTime);
                if (d.TotalSeconds >= 1)
                {
                    _lastTime = n;
                    _totalSeconds--;

                    if (_totalSeconds <= 0)
                    {
                        ScreenManager.AddScreen(_nextScreen);
                        _totalSeconds = 0;
                    }
                }
            }

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }
    }
}
